package com.zatom.newscore.utils;

import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class AppContext implements ApplicationContextAware {
  private static ApplicationContext applicationContext;
  
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    com.zatom.newscore.utils.AppContext.applicationContext = applicationContext;
  }
  
  public static <T> T getBean(Class<T> clasz) {
    return (applicationContext != null) ? (T)applicationContext.getBean(clasz) : null;
  }
  
  public static <T> Map<String, T> getBeanBySuper(Class<T> clasz) {
    return (applicationContext != null) ? applicationContext.getBeansOfType(clasz) : null;
  }
  
  public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
    return (applicationContext != null) ? applicationContext.getBeansWithAnnotation(annotationType) : null;
  }
}
